package com.example.yy.helloword0001;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

import java.lang.reflect.Field;

public class MyTextView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_text_view);

        TextView txtZQD = findViewById(R.id.txtZQD);
        Drawable[] drawable = txtZQD.getCompoundDrawables();
        // 数组下表0~3,依次是:左上右下
        drawable[1].setBounds(0, 0, 200, 200);
        txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
                drawable[3]);

        //TextView玩转HTML

        TextView html_one = findViewById(R.id.html_one);

        String st = "<font color='blue'><b>百度一下，你就知道~：</b></font><br>";
        st += "<a href = 'http://www.baidu.com'>百度</a>";
        html_one.setText(Html.fromHtml(st));
        //使得拥有交互功能
        html_one.setMovementMethod(LinkMovementMethod.getInstance());

        //测试src标签，插入图片

        TextView t2 = findViewById(R.id.html_two);
        String st2 = "图片：<a href='http://www.baidu.com'><img src='dog' /></a>";
        t2.setText(Html.fromHtml(st2,  new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                Drawable draw = null;
                try {
                    Field field = R.drawable.class.getField(source);
                    int resourceId = Integer.parseInt(field.get(null).toString());
                    draw = getResources().getDrawable(resourceId,null);
                    draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return draw;
            }
        }, null));

        t2.setMovementMethod(LinkMovementMethod.getInstance());

    }
}
